package com.wzq.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wzq.constant.SystemConstant;
import com.wzq.dao.pojo.Administrators;
import com.wzq.dao.pojo.R;
import com.wzq.service.IAdministratorsService;
import com.wzq.util.JwtUtils;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  管理员登录
 * </p>
 *
 * @author pzm
 * @since 2022-08-31
 */
@RestController
//@RequestMapping("/administrators")
public class AdministratorsController {


    @Autowired
    private IAdministratorsService iAdministratorsService;

    /**
     * 管理员登录
     * @param administrators
     * @return
     */
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody Administrators administrators){
        if(administrators==null){
            return R.error();
        }
        if(StringUtil.isEmpty(administrators.getUserName())){
            return R.error("用户名不能为空！");
        }
        if(StringUtil.isEmpty(administrators.getPassword())){
            return R.error("密码不能为空！");
        }
        Administrators resultAdmin = iAdministratorsService.getOne(new QueryWrapper<Administrators>().eq("userName", administrators.getUserName()));
        if(resultAdmin==null){
            return R.error("用户名不存在");
        }
        if(!resultAdmin.getPassword().trim().equals(administrators.getPassword())){
            return R.error("用户名或者密码错误！");
        }
        String token = JwtUtils.createJWT("-1", "admin", SystemConstant.JWT_TTL);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("token",token);
        return R.ok(resultMap);
    }

    /**
     * 修改密码
     * @param administrators
     * @return
     */
    @PostMapping("/admin/modifyPassword")
    public R modifyPassword(@RequestBody Administrators administrators){
        if(StringUtil.isEmpty(administrators.getUserName())){
            return R.error("用户名不能为空");
        }
        if(StringUtil.isEmpty(administrators.getPassword())){
            return R.error("新密码不能为空");
        }
        iAdministratorsService.update(new UpdateWrapper<Administrators>().set("password",administrators.getPassword()).eq("userName",administrators.getUserName()));
        return R.ok();
    }




}

